import { createRouter, createWebHashHistory } from "vue-router";
import { translate } from "@/locales/translatetion";
import { useUser } from "@/stores/user";
import { getToken } from "@/utils/request";
import bus from "@/utils/mitt";
import { Session } from "@/utils/storage";

const routes = [
  {
    path: "/", //默认页
    name: "",
    component: () => import("../layouts/baseLayout.vue"),
    children: [
      {
        path: "", //默认页
        name: "index",
        component: () => import("../views/index.vue"),
        meta: { title: translate("title.首页") },
      },
      {
        path: "goods/:id?",
        name: "goods",
        component: () => import("../views/goods.vue"),
        meta: { title: translate("title.商品详情"), needLogin: true },
      },
      {
        path: "store/:sellerOpenId?/:country?",
        name: "store",
        component: () => import("../views/store.vue"),
        meta: { title: translate("title.店铺"), needLogin: true },
      },
      {
        path: "search/:keyword?/",
        name: "search",
        component: () => import("../views/search.vue"),
        meta: { title: translate("title.搜索"), needLogin: true },
      },
      {
        path: "imageSearch",
        name: "imageSearch",
        component: () => import("../views/imageSearch.vue"),
        meta: { title: translate("title.搜索"), needLogin: true },
      },
      {
        path: "recommend/:id",
        name: "recommend",
        component: () => import("../views/recommend.vue"),
        meta: { title: translate("title.相似商品") },
      },
      {
        path: "cart",
        name: "cart",
        component: () => import("../views/cart.vue"),
        meta: { title: translate("title.购物车"), needLogin: true },
      },
      {
        path: "submitOrder",
        name: "submitOrder",
        component: () => import("../views/submitOrder.vue"),
        meta: { title: translate("title.提交订单"), needLogin: true },
      },
      {
        path: "orderInfo/:id",
        name: "orderInfo",
        component: () => import("../views/orderInfo.vue"),
        meta: { title: translate("title.订单详情"), needLogin: true },
      },
      // {
      //   path: "artical/:key",
      //   name: "artical",
      //   component: () => import("../views/artical.vue"),
      //   meta: {  },
      // },
      {
        path: "artical", //默认页
        name: "artical",
        component: () => import("../layouts/articalLayout.vue"),
        children: [
          {
            path: "content/:key?",
            name: "artical-content",
            component: () => import("../views/artical.vue"),
            meta: {},
          },
        ],
      },
      {
        path: "uc", //默认页
        name: "uc",
        redirect: "/uc/address",
        component: () => import("../layouts/userCenterLayout.vue"),
        children: [
          {
            path: "address",
            name: "uc-address",
            component: () => import("../views/addressManage.vue"),
            meta: { title: translate("button.收货地址"), needLogin: true },
          },
          {
            path: "orderList",
            name: "uc-orderList",
            component: () => import("../views/orderList.vue"),
            meta: { title: translate("button.我的订单"), needLogin: true },
          },
          {
            path: "helpCenter",
            name: "help-center",
            component: () => import("../views/helpCenter.vue"),
            meta: { title: translate("title.帮助中心"), needLogin: false },
          },
        ],
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0, behavior: "smooth" };
  },
});

router.beforeEach((to, from, next) => {
  // console.log("%c Line:83 🎂 to, from", "color:#465975", to, from);
  // 如果路由中有定义 meta.title，则使用它来设置页面标题
  if (to.meta) {
    if (to.meta.title) {
      document.title = `Alib2b - ${to.meta.title}`;
    } else {
      document.title = "Alib2b";
    }

    if (to.meta.needLogin) {
      //判断是否登录
      const token = getToken();
      if (!token) {
        Session.set("redirect_url", to.path); //用户登录成功后的跳转
        //未登录
        if (!from.href) {
          //直接访问 没有来源页 则直接跳转到首页 唤起登录框
          next("/?needLogin");
        } else {
          //路由跳转过来的  直接显示登录框
          bus.emit("showLoginDialog");
          next(false);
        }
        return;
      }
    }
  }

  //paypal支付回调页面跳转到订单详情
  if (to.path == "/paysucces") {
    const orderId = to.query.orderId;
    next(`/orderInfo/${orderId}`);
    return;
  }
  next();
});

// router.afterEach((to, from, next) => {
//   console.log("%c Line:114 🍩 to, from, next", "color:#ea7e5c", to, from, next);
//   if (to.fullPath.includes("needlogin")) {
//     bus.emit("showLoginDialog");
//   }
// });

export default router;
